
import UIKit
extension NSDate
{
    
    /// 获取当前时间的时间戳
    /// - Parameter format: 日期格式yyyy-MM-dd HH:mm:ss 或者 yyyy/MM/dd 自定义日期格式
    /// - Returns: 当前时间的时间戳
    @objc class func currentTimeStamp(_ format:String)->String
    {
        let formatter = DateFormatter()
        formatter.timeZone = NSTimeZone.local
        formatter.dateFormat = format
        let time = formatter.string(from:Date())
        let date = formatter.date(from:time)
        let stamp = "\(Int(date!.timeIntervalSince1970))"
        return stamp
    }
    ///时间戳(字符串形式)俺需要的格式，转换成日期格式(MM/dd，或者yy/MM/dd/)
    @objc class func transToDate(_ format : String, _ stamp:Int)->String
    {
        let timeInterval = TimeInterval(stamp)
        let date = Date(timeIntervalSince1970: timeInterval)
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = format
        return dateformatter.string(from: date)
    }
    /// 日期，按照固定格式转换成时间戳
    /// - Parameters:
    ///   - date: 需要转换的日期
    ///   - format: 格式
    /// - Returns: 时间戳
    @objc class func tranToStamp(_ date:String, _ format:String)->String{
        let formatter = DateFormatter()
        formatter.timeZone = NSTimeZone.local
        formatter.dateFormat = format
        let date = formatter.date(from: date)
        let stamp = "\(Int(date!.timeIntervalSince1970))"
        return stamp
    }
    /// 获取指定时间戳的年月日时分秒
    @objc class func dateOfStamp(_ stamp:NSInteger) ->DateComponents {
        let calendar = Calendar.current
        let date = stamp == 0 ? Date() : Date(timeIntervalSince1970: TimeInterval(stamp))
        let dateComponets = calendar.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day,Calendar.Component.hour,Calendar.Component.minute, Calendar.Component.second,.weekday], from: date)
        return dateComponets
    }
    
    /// 获取指定时间戳的月份的天数
    @objc class func numOfMonth(_ stamp:Int)->NSInteger
    {
        let date = stamp == 0 ? Date() : Date(timeIntervalSince1970:TimeInterval(stamp))
        let calendar = NSCalendar.current
        let range = calendar.range(of:.day, in: .month, for:date)
        return range?.count ?? 0
    }
    /// 获取指定时间戳的年份的天数
    @objc class func numOfYear(_ stamp:Int)->NSInteger
    {
        let date = stamp == 0 ? Date() : Date(timeIntervalSince1970:TimeInterval(stamp))
        let calendar = NSCalendar.current
        let range = calendar.range(of:.day, in: .year, for:date)
        return range?.count ?? 0
    }
    
    /// 获取指定时间戳的在这一年中的是第几天
    @objc class func dayInYear(_ stamp:NSInteger) -> Int {
        
        // 定义结果res
        // 定义月数组monthArr, 数组为每月天数
        var res = 0, monthArr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        
        let datecomponents =  stamp != 0 ? self.dateOfStamp(stamp) : self.dateOfStamp(Int(Date().timeIntervalSince1970))
        // 分割字符串获得年月日
        
        let year = datecomponents.year!, month = datecomponents.month!, day = datecomponents.day!
        
        // 闰年2月份为29天
        if year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) {
            monthArr[1] = 29
        }
        
        // 获得当前月之前的天数
        for i in 0 ..< month - 1 {
            res += monthArr[i]
        }
        return res + day
    }
    
    /// 获取指定时间戳的那天，那天的任意小时，整时的时间戳，如果当前时间还不到传入的当前整时，则返回当前最近的时间戳
    /// - Parameter hour: 需要转换的当前整时数
    /// - Returns: 时间戳
    @objc class func stamp(_ stamp:NSInteger, _ hour:Int)->NSInteger
    {
        var datecompnets = self.dateOfStamp(stamp)
        datecompnets.hour = hour
        datecompnets.minute = 0
        datecompnets.second = 0
        let newdate = Calendar.current.date(from: datecompnets)
        return NSInteger(newdate!.timeIntervalSince1970)
    }
    
    
    /// 获取指定时间是星期几
    /// - Parameter date: 指定时间
    /// - Returns: 返回星期的坐标，和星期，比如，（1，周日）
    @objc class func weakDay(_ date:NSDate)->(Int)
    {
        let calendar = NSCalendar.current
        let components = calendar.dateComponents([.weekOfMonth,.weekday], from: date as Date)
        return components.weekday!;
        //        switch (components.weekday)
        //        {
        //        case 1:
        //            return "周日"
        //        case 2:
        //            return "周一"
        //        case 3:
        //            return "周二"
        //        case 4:
        //            return "周三"
        //        case 5:
        //            return "周四"
        //        case 6:
        //            return "周五"
        //        case 7:
        //            return "周六"
        //        default:
        //            break
        //        }
        //        return ""
    }
    
    /// 指定时间戳，转换成半小时格式时间戳（1653953233 (2022-05-31 07:27:13) = 1653953400 (2022-05-31 07:30:00)）
    /// - Parameter stamp: 指定时间戳
    /// - Returns: 属于半小时格式的时间戳
    @objc class func halfHourFormStamp(_ stamp:Int)->Int
    {
        let date = NSDate.dateOfStamp(stamp)
        switch(date.minute! , date.second!)
        {
        case (30, 0),(0,0):
            return stamp
        case (..<30, _):
            let x = date.minute! * 60 + date.second!
            return stamp - x
        case (30..., _):
            let x = (date.minute! - 30) * 60 + date.second!
            return stamp - x
        default:
            return stamp
        }
    }
}

